package net.apps.std.spi;

import java.awt.AWTEvent;
import java.awt.Rectangle;
import java.awt.event.InputMethodEvent;
import java.awt.im.spi.InputMethod;
import java.awt.im.spi.InputMethodContext;
import java.lang.Character.Subset;
import java.text.AttributedString;
import java.util.Locale;

public class RemoteInputMethod implements InputMethod, RemoteInputControl {

	private InputMethodContext context;
	private Locale locale;
	private boolean active;
	private boolean disposed;
	
	@Override
	public void setInputMethodContext(InputMethodContext context) {
		this.context = context;
		this.locale = new Locale("remote");
	}

	@Override
	public boolean setLocale(Locale locale) {
		if (locale == null || !"remote".equals(locale.getLanguage()))
			return false;
		this.locale = locale;
		return true;
	}

	@Override
	public Locale getLocale() {
		return locale;
	}

	@Override
	public void setCharacterSubsets(Subset[] subsets) {
		// ignore
	}

	@Override
	public void setCompositionEnabled(boolean enable) {
		// ignore
	}

	@Override
	public boolean isCompositionEnabled() {
		return false;
	}

	@Override
	public void reconvert() {
		// ignore
	}

	@Override
	public void dispatchEvent(AWTEvent event) {
		// ignore
    }

	@Override
	public void notifyClientWindowChange(Rectangle bounds) {
		// ignore
	}

	@Override
	public void activate() {
		this.active = true;
	}

	@Override
	public void deactivate(boolean isTemporary) {
		this.active = false;
	}

	@Override
	public void hideWindows() {
	}

	@Override
	public void removeNotify() {
	}

	@Override
	public void endComposition() {
	}

	@Override
	public void dispose() {
	}

	@Override
	public Object getControlObject() {
		return this;
	}
	
	
	//
	// RemoteInputControl
	//
	
	public void putRemoteText(String text) {
		if (disposed)
			return;
		AttributedString as = new AttributedString(text);
	    context.dispatchInputMethodEvent(
	    		InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
                as.getIterator(),
                text.length(),
                null,
                null);
	}

}
